package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.TargetDao;
import cn.gson.springboot.model.mapper.aftermapper.TargetMapper;
import cn.gson.springboot.model.pjos.Dept;
import cn.gson.springboot.model.pjos.Emp;
import cn.gson.springboot.model.pjos.Target;
import cn.gson.springboot.model.vo.aftervo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;


@Service
@Transactional
public class TargetService {
    private List<TargetFinishVo> pieDoubleVoList;
    private List<TargetFinishVo> PieDoubleVoLists;
    private Double yearFirstValue;
    private Double yearFirstFinish;
    private Double yearSecondValue;
    private Double yearSecondFinish;
    private Double yearThirdValue;
    private Double yearThirdFinish;
    private List<TargetVo> firstValueDetails;
    private List<TargetVo> firstFinishDetails;
    private List<Double> chartValueList;
    private List<Double> chartFinishList;

    @Autowired
    TargetDao dao;
    @Autowired
    TargetMapper mapper;

    //销售目标值
    public List<TargetFinishVo> dateByValue(Integer date){
        pieDoubleVoList = mapper.dateByValue(date);
        return mapper.dateByValue(date);
    }

    //销售完成值
    public List<TargetFinishVo> dateByFinish(Integer date){
        PieDoubleVoLists = mapper.dateByFinish(date);
        return mapper.dateByFinish(date);
    }

    //全年目标与完成值
    public List<TargetFinishVo> finishValue(Integer date){
        pieDoubleVoList = dateByValue(date);
        PieDoubleVoLists = dateByFinish(date);
        List<TargetFinishVo> list = new LinkedList<>();
        if(pieDoubleVoList.size() != 0 && PieDoubleVoLists.size() == 0){
            for(int index=0;index<pieDoubleVoList.size();index++){
                TargetFinishVo pieDoubleVo = new TargetFinishVo();
                pieDoubleVo.setName(pieDoubleVoList.get(index).getName());
                pieDoubleVo.setValue(pieDoubleVoList.get(index).getValue());
                pieDoubleVo.setFinsValue(0.00);
                pieDoubleVo.setTranValue(0.00);
                list.add(pieDoubleVo);
            }
        }else {
            int ktp = 0;
            for(int index=0;index<pieDoubleVoList.size();index++){
                for(int i=0;i<PieDoubleVoLists.size();i++){
                    if(pieDoubleVoList.get(index).getName() == PieDoubleVoLists.get(i).getName()){
                        TargetFinishVo pieDoubleVo = new TargetFinishVo();
                        pieDoubleVo.setName(pieDoubleVoList.get(index).getName());
                        pieDoubleVo.setValue(pieDoubleVoList.get(index).getValue());
                        pieDoubleVo.setFinsValue(PieDoubleVoLists.get(i).getValue());
                        Double num = PieDoubleVoLists.get(i).getValue()/pieDoubleVoList.get(index).getValue();
                        pieDoubleVo.setTranValue(num);
                        list.add(pieDoubleVo);
                        ktp = index;
                    }
                }
            }
            for(int i=1;i<pieDoubleVoList.size();i++){
                if(i>ktp){
                    TargetFinishVo pieDoubleVo = new TargetFinishVo();
                    pieDoubleVo.setName(pieDoubleVoList.get(i).getName());
                    pieDoubleVo.setValue(pieDoubleVoList.get(i).getValue());
                    pieDoubleVo.setFinsValue(0.00);
                    pieDoubleVo.setTranValue(0.00);
                    list.add(pieDoubleVo);
                }
            }
        }
        return list;
    }

    //上半年销售目标值
    public Double firstYearValue(Integer date){
        yearFirstValue = mapper.firstYearByValue(date);
        return mapper.firstYearByValue(date);
    }

    //上半年销售完成值
    public Double firstYearFinish(Integer date){
        yearFirstFinish = mapper.firstYearByFinish(date);
        return mapper.firstYearByFinish(date);
    }

    //上半年目标与完成值
    public List<TargetFinishVo> firstValueFinish(Integer date){
        yearFirstValue = firstYearValue(date);
        yearFirstFinish = firstYearFinish(date);
        List<TargetFinishVo> list = new LinkedList<>();
        TargetFinishVo finishVo = new TargetFinishVo();
        if(yearFirstValue == null){
            finishVo.setValue(0.00);
            finishVo.setFinsValue(0.00);
            finishVo.setTranValue(0.00);
        }
        if(yearFirstFinish == null && yearFirstValue != null){
            finishVo.setValue(yearFirstValue);
            finishVo.setFinsValue(0.00);
            finishVo.setTranValue(0.00);
        }
        if(yearFirstFinish != null && yearFirstValue != null){
            finishVo.setValue(yearFirstValue);
            finishVo.setFinsValue(yearFirstFinish);
            finishVo.setTranValue(yearFirstFinish/yearFirstValue);
        }
        list.add(finishVo);
        return list;
    }

    //右侧销售目标值详情
    public List<TargetVo> firstYearValueDetails(Integer date, Integer month){
        firstValueDetails = mapper.firstYearByValueDetails(date,month);
        return mapper.firstYearByValueDetails(date,month);
    }

    //右侧销售完成值详情
    public List<TargetVo> firstYearFinishDetails(Integer date, Integer month){
        firstFinishDetails = mapper.firstYearByFinishDetails(date,month);
        return mapper.firstYearByFinishDetails(date,month);
    }

    //上半年销售目标值完成值详情
    public List<TargetVo> firstYearValueFinishDetails(Integer date, Integer month){
        firstValueDetails = firstYearValueDetails(date,month);
        firstFinishDetails = firstYearFinishDetails(date,month);
        List<TargetVo> list = new LinkedList<>();
        TargetVo targetVo = null;
        int x = 0;
        if(firstValueDetails.size() != 0 && firstFinishDetails.size() != 0){
          for(int i=0;i<firstValueDetails.size();i++){
              for(int r=x;r<firstFinishDetails.size();r++){
                  if(firstValueDetails.get(i).getEmployeesId().equals(firstFinishDetails.get(r).getEmployeesId())){
                      targetVo = new TargetVo();
                      targetVo.setEmployeesId(firstValueDetails.get(i).getEmployeesId());
                      targetVo.setDeptName(firstValueDetails.get(i).getDeptName());
                      targetVo.setEmpName(firstValueDetails.get(i).getEmpName());
                      targetVo.setValue(firstValueDetails.get(i).getValue());
                      targetVo.setFinish(firstFinishDetails.get(r).getFinish());
                      targetVo.setPercent(firstFinishDetails.get(r).getFinish()/firstValueDetails.get(i).getValue());
                      targetVo.setRemark(firstValueDetails.get(i).getRemark());
                      list.add(targetVo);
                      x = r+1;
                      break;
                  }
              }
          }
        }
        if(firstValueDetails.size() != 0 && firstFinishDetails.size() == 0){
            for(int i=0;i<firstValueDetails.size();i++){
                targetVo = new TargetVo();
                targetVo.setEmployeesId(firstValueDetails.get(i).getEmployeesId());
                targetVo.setDeptName(firstValueDetails.get(i).getDeptName());
                targetVo.setEmpName(firstValueDetails.get(i).getEmpName());
                targetVo.setValue(firstValueDetails.get(i).getValue());
                targetVo.setFinish(0.00);
                targetVo.setPercent(0.00);
                targetVo.setRemark(firstValueDetails.get(i).getRemark());
                list.add(targetVo);
            }
        }
        return list;
    }

    //下半年销售目标值
    public Double secondYearValue(Integer date){
        yearSecondValue = mapper.secondYearByValue(date);
        return mapper.secondYearByValue(date);
    }

    //下半年销售完成值
    public Double secondYearFinish(Integer date){
        yearSecondFinish = mapper.secondYearByFinish(date);
        return mapper.secondYearByFinish(date);
    }

    //下半年目标与完成值
    public List<TargetFinishVo> secondValueFinish(Integer date){
        yearSecondValue = secondYearValue(date);
        yearSecondFinish = secondYearFinish(date);
        List<TargetFinishVo> list = new LinkedList<>();
        TargetFinishVo finishVo = new TargetFinishVo();
        if(yearSecondValue == null){
            finishVo.setValue(0.00);
            finishVo.setFinsValue(0.00);
            finishVo.setTranValue(0.00);
        }
        if(yearSecondFinish == null && yearSecondValue != null){
            finishVo.setValue(yearSecondValue);
            finishVo.setFinsValue(0.00);
            finishVo.setTranValue(0.00);
        }
        if(yearSecondFinish != null && yearSecondValue != null){
            finishVo.setValue(yearSecondValue);
            finishVo.setFinsValue(yearSecondFinish);
            finishVo.setTranValue(yearSecondFinish/yearSecondValue);
        }
        list.add(finishVo);
        return list;
    }

    //全年销售完成值
    public Double thirdYearValue(Integer date){
        yearThirdValue = mapper.thirdYearByValue(date);
        return mapper.thirdYearByValue(date);
    }

    //全年销售目标值
    public Double thirdYearFinish(Integer date){
        yearThirdFinish = mapper.thirdYearByFinish(date);
        return mapper.thirdYearByFinish(date);
    }

    //全年目标完成值
    public List<TargetFinishVo> thirdValueFinish(Integer date){
        yearThirdValue = thirdYearValue(date);
        yearThirdFinish = thirdYearFinish(date);
        List<TargetFinishVo> list = new LinkedList<>();
        TargetFinishVo finishVo = new TargetFinishVo();
        if(yearThirdValue == null){
            finishVo.setValue(0.00);
            finishVo.setFinsValue(0.00);
            finishVo.setTranValue(0.00);
        }
        if(yearThirdFinish == null && yearThirdValue != null){
            finishVo.setValue(yearThirdValue);
            finishVo.setFinsValue(0.00);
            finishVo.setTranValue(0.00);
        }
        if(yearThirdFinish != null && yearThirdValue != null){
            finishVo.setValue(yearThirdValue);
            finishVo.setFinsValue(yearThirdFinish);
            finishVo.setTranValue(yearThirdFinish/yearThirdValue);
        }
        list.add(finishVo);
        return list;
    }

    //销售目标完成值
    public List<TargetVo> selectByMonth(Integer date){
        return mapper.infoByMonth(date);
    }

    //销售目标详情查询
    public List<TargetVo> selectTarget(Integer data, Integer month){
        return mapper.targetAll(data,month);
    }

    //详情员工名称
    public List<String> selectTargetByName(Integer data, Integer month){
        return mapper.targetByName(data,month);
    }

    //详情员工目标值
    public List<Double> selectTargetValue(Integer data, Integer month){
        return mapper.targetValue(data,month);
    }

    //详情员工完成值
    public List<Double> selectTargetFinish(Integer data, Integer month){
        return mapper.targetFinish(data,month);
    }

    //详情完成率
    public List<Double> selectTargetPercent(Integer data, Integer month){
        return mapper.targetPercent(data,month);
    }

    //部门下的员工
    public List<DeptVo> selectDeptEmp(){
        return mapper.deptEmp();
    }

    //新增销售目标
    public void addTarget(TargetSaveVo tar){
        Dept dept = new Dept();
        Emp emp = new Emp();
        Integer deptId = tar.getEmployeesId().get(0);
        Integer empId = tar.getEmployeesId().get(1);
        dept.setDeptsId(deptId);
        emp.setEmployeesId(empId);
        String years = null;
        if(tar.january != 0.0){//一月
            Target one = new Target();
            years = Integer.toString(tar.year);
            String date = years+"-"+"01"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            one.setDeptByDeptsId(dept);
            one.setEmpByEmployeesId(emp);
            one.setGoalValue(tar.getJanuary());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
            one.setGoalDate(ts);
            dao.save(one);
        }
        if(tar.february != 0.0){//二月
            Target two = new Target();
            years = Integer.toString(tar.year);
            String date2 = years+"-"+"02"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            two.setDeptByDeptsId(dept);
            two.setEmpByEmployeesId(emp);
            two.setGoalValue(tar.getFebruary());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            two.setGoalDate(ts);
            dao.save(two);
        }
        if(tar.march != 0.0){//三月
            Target three = new Target();
            years = Integer.toString(tar.year);
            String date3 = years+"-"+"03"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            three.setDeptByDeptsId(dept);
            three.setEmpByEmployeesId(emp);
            three.setGoalValue(tar.getMarch());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            three.setGoalDate(ts);
            dao.save(three);
        }
        if(tar.april != 0.0){//四月
            Target four = new Target();
            years = Integer.toString(tar.year);
            String date4 = years+"-"+"04"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            four.setDeptByDeptsId(dept);
            four.setEmpByEmployeesId(emp);
            four.setGoalValue(tar.getApril());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date4);
            } catch (Exception e) {
                e.printStackTrace();
            }
            four.setGoalDate(ts);
            dao.save(four);
        }
        if(tar.may != 0.0){//五月
            Target five = new Target();
            years = Integer.toString(tar.year);
            String date5 = years+"-"+"05"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            five.setDeptByDeptsId(dept);
            five.setEmpByEmployeesId(emp);
            five.setGoalValue(tar.getMay());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            five.setGoalDate(ts);
            dao.save(five);
        }
        if(tar.june != 0.0){//六月
            Target six = new Target();
            years = Integer.toString(tar.year);
            String date6 = years+"-"+"06"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            six.setDeptByDeptsId(dept);
            six.setEmpByEmployeesId(emp);
            six.setGoalValue(tar.getJune());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date6);
            } catch (Exception e) {
                e.printStackTrace();
            }
            six.setGoalDate(ts);
            dao.save(six);
        }
        if(tar.july != 0.0){//七月
            Target seven = new Target();
            years = Integer.toString(tar.year);
            String date7 = years+"-"+"07"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            seven.setDeptByDeptsId(dept);
            seven.setEmpByEmployeesId(emp);
            seven.setGoalValue(tar.getJuly());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date7);
            } catch (Exception e) {
                e.printStackTrace();
            }
            seven.setGoalDate(ts);
            dao.save(seven);
        }
        if(tar.august != 0.0){//八月
            Target eight = new Target();
            years = Integer.toString(tar.year);
            String date8 = years+"-"+"08"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            eight.setDeptByDeptsId(dept);
            eight.setEmpByEmployeesId(emp);
            eight.setGoalValue(tar.getAugust());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date8);
            } catch (Exception e) {
                e.printStackTrace();
            }
            eight.setGoalDate(ts);
            dao.save(eight);
        }
        if(tar.september != 0.0){//九月
            Target nine = new Target();
            years = Integer.toString(tar.year);
            String date9 = years+"-"+"09"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            nine.setDeptByDeptsId(dept);
            nine.setEmpByEmployeesId(emp);
            nine.setGoalValue(tar.getSeptember());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date9);
            } catch (Exception e) {
                e.printStackTrace();
            }
            nine.setGoalDate(ts);
            dao.save(nine);
        }
        if(tar.october != 0.0){//十月
            Target ten = new Target();
            years = Integer.toString(tar.year);
            String date10 = years+"-"+"10"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            ten.setDeptByDeptsId(dept);
            ten.setEmpByEmployeesId(emp);
            ten.setGoalValue(tar.getOctober());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date10);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ten.setGoalDate(ts);
            dao.save(ten);
        }
        if(tar.november != 0.0){//十一月
            Target eleven = new Target();
            years = Integer.toString(tar.year);
            String date11 = years+"-"+"11"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            eleven.setDeptByDeptsId(dept);
            eleven.setEmpByEmployeesId(emp);
            eleven.setGoalValue(tar.getNovember());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date11);
            } catch (Exception e) {
                e.printStackTrace();
            }
            eleven.setGoalDate(ts);
            dao.save(eleven);
        }
        if(tar.december != 0.0){//十二月
            Target twelve = new Target();
            years = Integer.toString(tar.year);
            String date12 = years+"-"+"12"+"-"+"01"+" "+"00"+":"+"00"+":"+"00";
            twelve.setDeptByDeptsId(dept);
            twelve.setEmpByEmployeesId(emp);
            twelve.setGoalValue(tar.getDecember());
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            try {
                ts = Timestamp.valueOf(date12);
            } catch (Exception e) {
                e.printStackTrace();
            }
            twelve.setGoalDate(ts);
            dao.save(twelve);
        }
    }

    //查看未完成目标
    public List<Target> selectTareByDate(String time, String empName){
        return mapper.tareByDate(time, empName);
    }

    //修改个人销售目标
    public void updateTarget(Integer goalId, double goalValue){
        Target tare = dao.findById(goalId).get();
        tare.setGoalValue(goalValue);
        dao.save(tare);
    }

    //查询员工一年中目标的月份
    public List<Integer> selectEmpByDate(Integer empId, Integer date){
        return mapper.empByDate(empId, date);
    }

    //查询员工目标最大月份
    public Integer selectDateByMax(Integer empId,Integer date){
        return mapper.dateByMax(empId,date);
    }

    //右侧月份图表月份名
    public List<String> selectChartByName(Integer date, Integer month){
        return mapper.chartByName(date, month);
    }

    //右侧月份图表目标值
    public List<Double> selectChartByValue(Integer date, Integer month){
        chartValueList = mapper.chartByValue(date, month);
        return mapper.chartByValue(date, month);
    }

    //右侧月份图表完成值
    public List<Double> selectChartByFinish(Integer date, Integer month){
        chartFinishList = mapper.chartByFinish(date, month);
        return mapper.chartByFinish(date, month);
    }

    //右侧月份图表目标与完成对比
    public List<Double> chartValueFinish(Integer date, Integer month){
        chartValueList = selectChartByValue(date, month);
        chartFinishList = selectChartByFinish(date, month);
        List<Double> list = new ArrayList<>();
        Double percent = 0.00;
        Double percents = 0.00;
        for(int i=0;i<this.chartFinishList.size();i++){
            percent = chartFinishList.get(i)/chartValueList.get(i);
            percents = (double) Math.round(percent * 100) / 100;
            list.add(percents);
        }
        return list;
    }

    //部门销售目标值
    public List<PieDoubleVo> selectDeptValue(Integer date, Integer month){
        return mapper.deptByValue(date, month);
    }

    //部门销售目标完成值
    public List<PieDoubleVo> selectDeptFinish(Integer date, Integer month){
        return mapper.deptByFinish(date, month);
    }
}
